//
//  AppDelegate+Umeng.m
//  JewelryStore
//
//  Created by lcs on 16/10/13.
//  Copyright © 2016年 com. All rights reserved.
//

#import "AppDelegate+Umeng.h"
#import <UMSocialCore/UMSocialCore.h>
#import "UMSocialUIManager.h"


@implementation AppDelegate (Umeng)
+ (void)setupUmengShare
{
    //打开调试日志
    //TODO:上线时得关闭
    [[UMSocialManager defaultManager] openLog:NO];
    
    //设置友盟appkey
    [[UMSocialManager defaultManager] setUmSocialAppkey:KUmengAPPKey];
    
    //设置微信的appKey和appSecret
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:KWeChatAPPKey appSecret:KWeChatAPPSecret redirectURL:@"http://mobile.umeng.com/social"];
    
    //设置分享到QQ互联的appKey和appSecret
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ appKey:KQQAPPKey  appSecret:KQQAPPSecret redirectURL:@"http://mobile.umeng.com/social"];
    
    //设置新浪的appKey和appSecret
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Sina appKey:KSinaAPPKey  appSecret:KSinaAPPSecret redirectURL:@"http://sns.whalecloud.com/sina2/callback"];
    
    // 如果不想显示平台下的某些类型，可用以下接口设置
    //    [[UMSocialManager defaultManager] removePlatformProviderWithPlatformTypes:@[@(UMSocialPlatformType_WechatFavorite),@(UMSocialPlatformType_YixinTimeLine),@(UMSocialPlatformType_LaiWangTimeLine),@(UMSocialPlatformType_Qzone)]];
    

}

+ (BOOL)UmengCallbackWithURL:(NSURL *)url
{
    BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url];
    if (!result) {
        //支付宝等SDK回调
    }
    return result;
}

+ (void)showUmengShareMenuViewWithShareinfo:(ShareInfo *)info
{
    //显示分享面板
    __weak typeof(self) weakSelf = self;
    [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMShareMenuSelectionView *shareSelectionView, UMSocialPlatformType platformType) {
        [weakSelf shareContentToPlatformType:platformType info:info];
    }];
}

#pragma mark - private
//分享
+ (void)shareContentToPlatformType:(UMSocialPlatformType)platformType info:(ShareInfo *)info
{
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    info.messageObject = messageObject;
    ShareInfo *object = [self shareContentWithShareInfo:info];
    //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:object.messageObject currentViewController:nil completion:^(id data, NSError *error) {
        if (error) {
            TT_Log(@"************Share fail with error %@*********",error);
        }else{
            if ([data isKindOfClass:[UMSocialShareResponse class]]) {
                UMSocialShareResponse *resp = data;
                //分享结果消息
                TT_Log(@"response message is %@",resp.message);
                //第三方原始返回的数据
                TT_Log(@"response originalResponse data is %@",resp.originalResponse);
                
            }else{
                TT_Log(@"response data is %@",data);
            }
        }
        [self alertWithError:error];
    }];
}
+ (ShareInfo *)shareContentWithShareInfo:(ShareInfo *)info
{
    if (info.type == UmengShareType_title)
    {
        info.messageObject.text = info.title;
        
    }else if (info.type == UmengShareType_image)
    {
        info.imageObject.thumbImage = info.thumbImage;
        info.imageObject.shareImage = info.image;
        info.messageObject.shareObject = info.imageObject;
        
    }else if (info.type == UmengShareType_imageAndTitle)
    {
        info.messageObject.text = info.title;
        info.imageObject.thumbImage = info.thumbImage;
        info.imageObject.shareImage = info.image;
        info.messageObject.shareObject = info.imageObject;
    
    }else if (info.type == UmengShareType_WebPage)
    {
        info.webpageObject.title = info.title;
        info.webpageObject.descr = info.content;
        info.webpageObject.thumbImage = info.thumbImage;
        info.webpageObject.webpageUrl = info.webPageUrl;
        info.messageObject.shareObject = info.webpageObject;
        
    }else if (info.type == UmengShareType_Music)
    {
        info.musicObject.title = info.title;
        info.musicObject.descr = info.content;
        info.musicObject.thumbImage = info.thumbImage;
        info.musicObject.musicUrl = info.musicUrl;
        info.messageObject.shareObject = info.musicObject;
        
    }else if (info.type == UmengShareType_Vedio)
    {
        info.videoObject.title = info.title;
        info.videoObject.descr = info.content;
        info.videoObject.thumbImage = info.thumbImage;
        info.videoObject.videoUrl = info.videoUrl;
        info.messageObject.shareObject = info.videoObject;
    }
    
    return info;
}
+ (void)alertWithError:(NSError *)error
{
    NSString *result = nil;
    if (!error) {
        result = [NSString stringWithFormat:@"分享成功"];
    }
    else{
        if (error) {
            result = [NSString stringWithFormat:@"分享失败，原因: %d\n",(int)error.code];
        }
        else{
            result = [NSString stringWithFormat:@"分享失败"];
        }
    }
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"share"
                                                    message:result
                                                   delegate:nil
                                          cancelButtonTitle:NSLocalizedString(@"sure", @"确定")
                                          otherButtonTitles:nil];
    [alert show];
}

@end
